import pandas as pd
from prefect import get_run_logger

from application.db.mysql_db.info import ResourceInformationList, ResourceSourceDict, ResourceInformationSectionList, \
    ResourceInformationTagsRelation, ResourceInformationAttachmentList
from application.db.mysql_db.nsfc.NsfcInfoList import NsfcInfoList
from application.db.mysql_db.nsfc.NsfcInfoTypeDict import NsfcInfoTypeDict
from application.db.mysql_db.nsfc.NsfcResourceSourceDict import NsfcResourceSourceDict
from application.tasks.base_task import BaseTask


class DataTypeGroupingTask(BaseTask):
    apply_code_dict = {
        "数理科学部": "A",
        "数学物理科学部": "A",
        "化学科学部": "B",
        "生命科学部": "C",
        "地球科学部": "D",
        "工程与材料科学部": "E",
        "信息科学部": "F",
        "管理科学部": "G",
        "医学科学部": "H",
    }  # 学部领域代码字典

    def run(self, df: pd.DataFrame):
        info_type_dict = {item.info_type_name: item.info_type_id for item in
                          NsfcInfoTypeDict.select(NsfcInfoTypeDict.info_type_id,
                                                  NsfcInfoTypeDict.info_type_name)}  # 信息类型

        # 计算 info_type_id 字段
        df["tag_value"] = df["tag_value"].apply(lambda x: eval(x))

        df["info_type_id"] = df["tag_value"].apply(self._get_info_type_id, args=(info_type_dict,))

        # 计算 apply_code 字段
        df["apply_code"] = df["tag_value"].apply(self._get_apply_code)
        return df

    def _get_info_type_id(self, tag_value, info_type_dict):
        """
        从 tag_value 中提取信息类型编号。

        :param tag_value: 包含信息类型与学部代码的列表或元组
        :param info_type_dict: 信息类型名称到编号的映射字典
        :return: 对应的信息类型编号，若无法匹配则返回 None
        """
        try:
            if isinstance(tag_value, (list, tuple)) and len(tag_value) >= 1:
                return info_type_dict.get(tag_value[0])
        except Exception:
            pass
        return None

    def _get_apply_code(self, tag_value):
        """
        从 tag_value 中提取学部领域代码。

        :param tag_value: 包含信息类型与学部代码的列表或元组
        :return: 对应的学部领域代码，若无法匹配则返回 "*"
        """
        try:
            if isinstance(tag_value, (list, tuple)) and len(tag_value) >= 2:
                return self.apply_code_dict.get(tag_value[1], "*")
        except Exception:
            pass
        return "*"

#
# if __name__ == '__main__':
#     from peewee import JOIN
#
#     nsfc_info_fields = [
#         NsfcInfoList.information_id  # 国自然基金已有信息的 ID
#     ]
#     nsfc_resource_fields = [
#         NsfcResourceSourceDict.source_id,
#         NsfcResourceSourceDict.source_main_link  # 国自然基金资源表主链接
#     ]
#
#     info_list_fields = [
#         ResourceInformationList.information_id,
#         ResourceInformationList.source_id,
#         ResourceInformationList.information_name,
#         ResourceInformationList.information_description,
#         ResourceInformationList.original_language,
#         ResourceInformationList.cover,
#         ResourceInformationList.original_link,
#         ResourceInformationList.publish_date  # 主表字段，用于基础信息
#     ]
#     source_dict_fields = [
#         ResourceSourceDict.source_id,
#         ResourceSourceDict.source_main_link  # 资源来源字典字段
#     ]
#     info_section_fields = [
#         ResourceInformationSectionList.section_id,
#         ResourceInformationSectionList.information_id,
#         ResourceInformationSectionList.section_order,
#         ResourceInformationSectionList.section_attr,
#         ResourceInformationSectionList.title_level,
#         ResourceInformationSectionList.marc_code,
#         ResourceInformationSectionList.src_text,
#         ResourceInformationSectionList.dst_text,
#         ResourceInformationSectionList.media_info  # 资讯章节相关字段
#     ]
#     info_tags_relation_fields = [
#         ResourceInformationTagsRelation.information_id,
#         ResourceInformationTagsRelation.tag_code,
#         ResourceInformationTagsRelation.tag_value  # 标签关联字段
#     ]
#
#     info_attachment_fields = [
#         ResourceInformationAttachmentList.attachment_id,
#         ResourceInformationAttachmentList.information_id,
#         ResourceInformationAttachmentList.attachment_name,
#         ResourceInformationAttachmentList.attachment_address,
#         ResourceInformationAttachmentList.display_order  # 附件字段
#     ]
#
#     # 查询国自然基金资源表，转换为 DataFrame
#     nsfc_resource = pd.DataFrame(NsfcResourceSourceDict.select(*nsfc_resource_fields).dicts())
#     # 查询资源来源字典，转换为 DataFrame
#     source_dict = pd.DataFrame(ResourceSourceDict.select(*source_dict_fields).dicts())
#     # 查询国自然基金已有信息列表
#     nsfc_list = pd.DataFrame(NsfcInfoList.select(*nsfc_info_fields).dicts())
#
#     # 将资讯湖资源与国自然基金资源进行合并
#     merged_df = pd.merge(
#         source_dict,
#         nsfc_resource,
#         on="source_main_link",  # 使用主链接作为合并键
#         how="inner",  # 仅保留两者匹配的记录
#         suffixes=("_info", "_nsfc")  # 避免 source_id 字段冲突
#     )
#
#     # 获取资讯湖中需要处理的 source_id 列表
#     source_id_info_list = merged_df['source_id_info'].tolist() if not merged_df.empty else []
#     # 获取国自然基金已存在的信息 ID 列表
#     information_id_list = nsfc_list['information_id'].tolist() if not nsfc_list.empty else []
#
#     # 查询资讯湖章节表，排除已存在国自然基金的信息
#     info_section = pd.DataFrame(ResourceInformationSectionList.select(*info_section_fields).dicts().where(
#         ~ResourceInformationSectionList.information_id.in_(information_id_list)
#     ))
#
#     # 查询资讯湖附件表，排除已存在国自然基金的信息
#     info_attachment = pd.DataFrame(ResourceInformationAttachmentList.select(*info_attachment_fields).where(
#         ~ResourceInformationAttachmentList.information_id.in_(information_id_list)
#     ).dicts())
#
#     # 按 information_id 分组，将章节表转换为字典列表，若为空则返回空字典
#     section_list = (
#         info_section.groupby('information_id', group_keys=False)
#         .apply(lambda x: x.to_dict(orient='records'))
#         if not info_section.empty else {}
#     )
#
#     # 按 information_id 分组，将附件表转换为字典列表，若为空则返回空字典
#     attachment_list = (
#         info_attachment.groupby('information_id', group_keys=False)
#         .apply(lambda x: x.to_dict(orient='records'))
#         if not info_attachment.empty else {}
#     )
#
#     # 查询资讯湖主表及标签关联表，左连接获取标签信息，排除已存在国自然基金的信息
#     query = (
#         ResourceInformationList
#         .select(
#             *info_list_fields,
#             *info_tags_relation_fields,
#         )
#         .join(ResourceInformationTagsRelation, JOIN.LEFT_OUTER,
#               on=(ResourceInformationList.information_id == ResourceInformationTagsRelation.information_id)
#               )
#         .where(
#             ResourceInformationList.source_id.in_(source_id_info_list),
#             ~ResourceInformationList.information_id.in_(information_id_list)  # 排除已存在信息
#         )
#         .dicts()
#     )
#
#     # 转换为 DataFrame
#     info_list = pd.DataFrame(query)
#
#     # 将章节和附件信息映射到主表中
#     info_list['info_section'] = info_list['information_id'].map(section_list)
#     info_list['info_attachment'] = info_list['information_id'].map(attachment_list)
#
#     dun = DataTypeGroupingTask()
#     dd = dun.run(info_list)
#     print(dd)
